package dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import model.ModelCustomer;
import table.TabModCustomer;
import control.CtrConsultCustomer;
import dao.Customer;


//CRIAR UM CONTROL
public class DlgConsultCustomer extends JDialog {
	private JPanel contentPanel;
	
	private JPanel pnlNorth;
	private JTextField txtFilter;
	private JButton btnSearch;
	
	private JScrollPane scpCenter;
	private JTable tblCustomer;
	private ArrayList<Customer> colCustomer = new ArrayList<Customer>();
	private TabModCustomer modelTable;
	
	private JPanel pnlSouth;
	private FormLayout fLSouth;
	private JButton btnNew;
	private JButton btnConfirm;
	private JButton btnCancel;
	
	private CtrConsultCustomer control;
	private ModelCustomer model;
	
	public DlgConsultCustomer(ArrayList<Customer> colCustomer, String name) {
		this.setTitle("Selecionar Cliente");
		this.setSize(600,400);
		
		this.control = new CtrConsultCustomer(this);

		this.colCustomer = colCustomer;
		this.add(getContentPanel());		
		getTxtFilter().setText(name);
		
		tblCustomer.grabFocus();		
		
		this.setLocationRelativeTo(null);
		
		this.setModal(true);
		this.setVisible(true);
	}
	
	private JPanel getContentPanel(){
		if(contentPanel == null){
			contentPanel = new JPanel(new BorderLayout());	
			contentPanel.add("North",getPnlNorth());
			contentPanel.add("Center",getScpCenter());
			contentPanel.add("South",getPnlSouth());
		}
		return contentPanel;
	}
	
	private JPanel getPnlNorth() {
		if(pnlNorth == null){			
			pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEFT));
			pnlNorth.add(new JLabel("Nome:"));
			pnlNorth.add(getTxtFilter());
			pnlNorth.add(getBtnSearch());
		}
		return pnlNorth;
	}

	private JTextField getTxtFilter() {
		if(txtFilter == null){			
			txtFilter = new JTextField(12);
			txtFilter.setName("txtFilter");
			txtFilter.addKeyListener(control);
		}
		return txtFilter;
	}

	private JButton getBtnSearch() {
		if(btnSearch == null){			
			btnSearch = new JButton("Pesquisar", new ImageIcon(ClassLoader.getSystemResource("img/icon_search.PNG")));
			btnSearch.setMnemonic('P');
			btnSearch.setName("btnSearch");
			btnSearch.addActionListener(control);
		}
		return btnSearch;
	}

	private JPanel getPnlSouth() {
		if(pnlSouth == null){				
			pnlSouth = new JPanel(getFLSouth());
			CellConstraints cc = new CellConstraints();

			pnlSouth.add(getBtnNew()	, cc.xy (1, 2));
			pnlSouth.add(getBtnConfirm(), cc.xy (3, 2));
			pnlSouth.add(getBtnCancel() , cc.xy (5, 2));
		}
		return pnlSouth;
	}

	private FormLayout getFLSouth() {
		if(fLSouth == null){
			fLSouth = new FormLayout("pref, 146dlu, pref, 5dlu, pref", "6dlu, pref");
		}
		return fLSouth;
	}
	
	private JButton getBtnNew() {
		if(btnNew == null){
			btnNew = new JButton("Novo", new ImageIcon(ClassLoader.getSystemResource("img/icon_add.PNG")));
			btnNew.setName("btnNew");
			btnNew.setMnemonic('N');
			btnNew.addActionListener(control);
		}
		return btnNew;
	}
	
	private JButton getBtnConfirm() {
		if(btnConfirm == null){			
			btnConfirm = new JButton("Confirmar", new ImageIcon(ClassLoader.getSystemResource("img/icon_ok.PNG")));	
			btnConfirm.setName("btnConfirm");
			btnConfirm.setMnemonic('F');
			btnConfirm.addActionListener(control);
		}
		return btnConfirm;
	}

	private JButton getBtnCancel() {
		if(btnCancel == null){			
			btnCancel = new JButton("Cancelar", new ImageIcon(ClassLoader.getSystemResource("img/icon_canc.PNG")));
			btnCancel.setName("btnCancel");
			btnCancel.setMnemonic('C');
			btnCancel.addActionListener(control);
		}
		return btnCancel;
	}
	
	public JTable getTblCustomer() {
		if(tblCustomer == null){			
			modelTable = new TabModCustomer(colCustomer);	
			tblCustomer = new JTable(modelTable);
			tblCustomer.setName("tblCustomer");
			tblCustomer.addKeyListener(control);
			tblCustomer.getColumnModel().getColumn(0).setMinWidth(60);
			tblCustomer.getColumnModel().getColumn(0).setMaxWidth(60);
		}
		return tblCustomer;
	}
	
	public JScrollPane getScpCenter() {
		if(scpCenter == null){
			scpCenter = new JScrollPane(getTblCustomer());
		}
		
		return scpCenter;
	}
	
	public Customer getCustomer(){
		if(colCustomer != null){
			if (tblCustomer.getSelectedRow() >= 0){
				return colCustomer.get(tblCustomer.getSelectedRow());
			}
		}
		return null;
	}
	
	public void setText(String text){
		txtFilter.setText(text);
	}
	
	public String getText(){		
		return txtFilter.getText().trim();		
	}
	
	public int getSelectedRow(){
		return tblCustomer.getSelectedRow();
	}
	
	public void setNull(){
		this.colCustomer = null;
	}
		
	public void update(ArrayList<Customer> colCustomer) {
		if(colCustomer != null){
			this.colCustomer = colCustomer;
			modelTable.setCustomer(colCustomer);		
		}
	}
}
